const {ccclass, property} = cc._decorator;

@ccclass
export default class Game extends cc.Component {
    @property(cc.Node)
    bigBall:cc.Node = undefined

    @property(cc.Prefab)
    shootBall:cc.Prefab = undefined

    @property(cc.Prefab)
    shoot:cc.Prefab = undefined

    @property(cc.Label)
    score:cc.Label = undefined

    private r:number = 250

    private shootBallPool:cc.Node[] = []

    public speed:number = 5

    private curIndex:number = 0

    public isFirst:boolean = true


    private tweenActionNode:cc.Node = null

    onLoad () {
        this.init()
        this.initBall()
        this.initShootBall()
        this.AddEventListen()
    }

    init(){
        this.initScene()
    }

    private initScene(){
        this.score.string = 0 + ''
    }

    private initBall(){
        cc.director.getCollisionManager().enabled = true
        cc.tween(this.bigBall).repeatForever(
            cc.tween().to(1,{scale:0.7}).to(1,{scale:1}).to(1,{scale:1.2}).to(1,{scale:1})
        ).start()
    }

    public randomBall(){
        for(let i = 0;i<6;i++){
            this.shootBallPool[i].opacity = 20
        }
        this.initBallState()
    }

    private initShootBall(){
        let y = Math.sqrt(Math.pow(this.r,2) - Math.pow(this.r/2,2))
        let pos1 = {x:-this.r/2,y:y}
        let pos2 = {x:this.r/2,y:y}
        let pos3 = {x:this.r,y:0}
        let pos4 = {x:this.r/2,y:-y}
        let pos5 = {x:-this.r/2,y:-y}
        let pos6 = {x:-this.r,y:0}
        for(let i = 0;i < 6;i++){
            let node = cc.instantiate(this.shootBall)
            node.parent = this.node
            let pos = undefined
            switch(i){
                case 0:pos = pos1;break;
                case 1:pos = pos2;break;
                case 2:pos = pos3;break;
                case 3:pos = pos4;break;
                case 4:pos = pos5;break;
                case 5:pos = pos6;break;
            }
            node.x = pos.x
            node.y = pos.y
            node.opacity = 20
            node.getComponent('shootBall').init(this)
            this.shootBallPool.push(node)
        }
    }

    start () {
       this.initBallState()
    }

    private AddEventListen(){
        this.node.on('touchstart',this.touchStart,this)
    }

    private touchStart(){
        if(this.tweenActionNode){
            cc.Tween.stopAllByTarget(this.tweenActionNode)
            this.tweenActionNode.scale = 1
            this.tweenActionNode.active = false
        }
        let node = cc.instantiate(this.shoot)
        let ball = this.shootBallPool[this.curIndex]
        node.setPosition(ball.x,ball.y)
        node.parent = this.node
        node.getComponent('shoot').init(this,ball.getChildByName('ball'))
    }

    private initBallState(){
        let n = this.getRandomNum()
        this.curIndex = n
        let node = this.shootBallPool[n]
        node.opacity = 255
        node.getChildByName('ball').getChildByName('block').getChildByName('line').active = true
        if(this.isFirst){
            this.isFirst = false
        }else{
            let block = node.getChildByName('timeBlock')
            block.active = true
            this.tweenActionNode = block
            cc.tween(block).to(3,{scale:0}).call(()=>{this.checkTouch()}).start()
            
        }

    }

    private checkTouch(){
            this.init()
            this.isFirst = true
            this.randomBall()
    }

    public setScore(){
        this.score.string = +this.score.string + 1 + ''
    }

    private getRandomNum():number{
        let n = Math.floor(Math.random()*6+1)
        if(n-1 == this.curIndex) n = this.getRandomNum()
        return n-1
    }
}
